<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2019 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Order.php 92886 2019-01-28 15:22:15 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_order
 * +----------------------------------------------------------------------
 */

namespace application\member\controller;

use application\admin\controller\base as auth;
use ticky\request;
use ticky\response;

class order extends auth {

    public $paytype = array('1' => '支付宝', '2' => '微信');  //支付类型
    public $order_status = array('0' => '未付款', '1' => '已付款');  //订单状态

    // 首页

    public function index() {
        $search = $this->search_frm();
        $ret = db('order')->where($search['sql'])->order('id desc')->newpage();
        foreach ($ret['items'] as $row) {
            if ($row['paytime'] == 0) {
                $row['paytime'] = '<span style="color: red;">未支付</span>';
            } else {
                $row['paytime'] = date("Y-m-d H:i:s", $row['paytime']);
            }
            $row['status'] = $this->order_status[$row['status']];
            $row['paytype'] = $this->paytype[$row['paytype']];
            $data[] = $row;
        }
        $this->assign('page', $ret['page']);
        $this->assign('items', $data);
        $this->assign('search', $search['arr']);
        $this->display('order/order_list');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            db('order')->insert($data);
            showmsg('添加成功', '/admin/order');
        } else {
            $this->assign('postUrl', '/admin/order/add');
            $this->assign('action', '添加');
            $this->display('order/manage');
        }
    }

    // 更新记录
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            $id = request::post('id', 0);
            db('order')->where(['id' => $id])->update($data);
            showmsg('修改成功', '/admin/order');
        } else {
            $id = request::get('id', 0);
            $item = db('order')->where(['id' => $id])->find();
            $this->assign('postUrl', '/admin/order/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
            $this->display('order/manage');
        }
    }

    // 删除记录
    public function delete() {
        $id = request::get('id', 0);
        $result = db('order')->delete(['id' => $id]);
        showmsg('删除成功', '/admin/order');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('order')->delete(['id' => $id]);
            }
            showmsg('删除成功', '/admin/order');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';
        if ($id = trim($search['id'])) {
            $where .= "and id = '{$id}' ";
        }
        if ($order_sn = trim($search['order_sn'])) {
            $where .= "and order_sn = '{$order_sn}' ";
        }
        if ($status = trim($search['status'])) {
            $where .= "and status = '{$status}' ";
        }
        if ($userid = trim($search['userid'])) {
            $where .= "and userid = '{$userid}' ";
        }
        if ($username = trim($search['username'])) {
            $where .= "and username = '{$username}' ";
        }
        if ($addtime = trim($search['addtime'])) {
            $where .= "and addtime = '{$addtime}' ";
        }
        if ($paytime = trim($search['paytime'])) {
            $where .= "and paytime = '{$paytime}' ";
        }
        if ($paytype = trim($search['paytype'])) {
            $where .= "and paytype = '{$paytype}' ";
        }
        if ($transaction = trim($search['transaction'])) {
            $where .= "and transaction = '{$transaction}' ";
        }
        if ($money = trim($search['money'])) {
            $where .= "and money = '{$money}' ";
        }
        if ($quantity = trim($search['quantity'])) {
            $where .= "and quantity = '{$quantity}' ";
        }
        if ($ip = trim($search['ip'])) {
            $where .= "and ip = '{$ip}' ";
        }
        if ($desc = trim($search['desc'])) {
            $where .= "and desc = '{$desc}' ";
        }
        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['order_sn'] = request::post('order_sn', '');
        $arr['status'] = request::post('status', '');
        $arr['userid'] = request::post('userid', '');
        $arr['username'] = request::post('username', '');
        $arr['addtime'] = request::post('addtime', '');
        $arr['paytime'] = request::post('paytime', '');
        $arr['paytype'] = request::post('paytype', '');
        $arr['transaction'] = request::post('transaction', '');
        $arr['money'] = request::post('money', '');
        $arr['quantity'] = request::post('quantity', '');
        $arr['ip'] = request::post('ip', '');
        $arr['desc'] = request::post('desc', '');
        return $arr;
    }

    // 订单改价
    public function change_price() {
        if (isset($_POST['dosubmit'])) {
            $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
            $money = isset($_POST['money']) ? floatval($_POST['money']) : 0;
            if ($money < 0.01) {
                showmsg('支付金额不能小于0.01元', '/admin/order');
            }
            db('order')->where(['id' => $id])->update(array('money' => $money));
            showmsg('订单改价成功', '/admin/order');
        }
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $data = db('order')->where(array('id' => $id))->find();
        $this->assign('item', $data);
        $this->display('order/change_price');
    }

    // 订单详情
    public function details() {
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $data = db('order')->where(array('id' => $id))->find();
        $this->assign('item', $data);
        $this->display('order/order_details');
    }

}
